package com.itheima.controller;


import com.itheima.common.PageBean;
import com.itheima.common.Result;
import com.itheima.pojo.Classes;
import com.itheima.service.ClassService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@Slf4j
@Component
@RestController
@RequestMapping("/classes")
public class ClassController {
    @Autowired
    private ClassService classServiceImpl;



    /**
     * 增加一个班级
     * @param classes
     * @return
     */
    @PostMapping
    public Result insertOne(@RequestBody Classes classes){
        classServiceImpl.insertOne(classes);
        return Result.success();
    }

    /**
     * 删除一个班级
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteOne(@PathVariable Integer id){
        classServiceImpl.deleteOne(id);

        return Result.success();
    }

    /**
     * 根据id查询一个学生
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectOne(@PathVariable Integer id){
        Classes classes = classServiceImpl.selectOne(id);

        return Result.success(classes);
    }

    /**
     * 修改学生信息
     * @param classes
     * @return
     */
    @PutMapping
    public Result updateOne(@RequestBody Classes classes){
        classServiceImpl.updateOne(classes);

        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Classes>> selectPage(
            @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "1")Integer pageSize,
            String name,
            @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end
            ){
        PageBean<Classes> classesPageBean = classServiceImpl.selectPage(
                page,pageSize,name,begin,end
        );

        return Result.success(classesPageBean);

    }


}
